<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * display_override
 *
 * @author Simon Emms <simon@simonemms.com>
 */

class display_override {
    
    
    
    
    /**
     * Format Display
     * 
     * Formats and outputs the final display. Any
     * variables that need to be displayed in the
     * main template go here.
     * 
     * It emulates Smarty comments in the HTML file,
     * which is useful for private commenting for
     * developers
     */
    public function format_display() {
        $objCI = &get_instance();
        
        /* Save the referrer session */
        $objCI->session->set_flashdata($objCI->config->item('session_referrer'), current_url());
        
        /* Check that the output has been called */
        if(is_null($objCI->output->get_output()) === false) {
        
            /* Load stuff */
            $objCI->load->config('output');
            $objCI->load->form('general');
            $objCI->lang->load('page');

            /* Decide the template */
            $template = 'main'; /* Default */
            $arrChange = $objCI->config->item('master_template');

            $arrUrl = array(
                $objCI->router->directory,
                $objCI->router->class,
                $objCI->router->method,
            );

            /* Get string and remove starting / */
            $url = preg_replace('/^\//', '', implode('/', $arrUrl));

            if(array_key_exists($url, $arrChange)) {
                $template = $arrChange[$url];
            }

            /* Search for page title */
            $pagetitle = $objCI->load->get_var('pagetitle');
            if(is_null($pagetitle) || $pagetitle === false) { $pagetitle = $objCI->lang->line('default_pagetitle'); }
            
            /* Logged in stuff */
            $logged_in = $objCI->database->verify_saved_login();
            
            /* Get the connections */
            if($logged_in) {
                $new_connection_page = $objCI->router->fetch_class() == 'home' && $objCI->router->fetch_method() == 'new_connection';
                
                $arrConnections = array();
                $arrRawConnections = $objCI->database->fetch_connections();
                if(count($arrRawConnections) > 0) {
                    $connection_id = $objCI->database->db(true);
                    foreach($arrRawConnections as $id => $connection) {
                        $arrConnections[$id] = array(
                            'name' => $connection['name'],
                            'url' => $objCI->input->rest_replace($objCI->config->item('connection', 'rest'), $id, site_url()),
                            'selected' => $id == $connection_id && $new_connection_page === false,
                            'type' => $connection['type'],
                        );
                    }
                }
            }
            
            
            /* Get the available languages */
            $arrFormLang = $objCI->general_form->language();

            /** Display Data **/
            $arrDisplay = array(
                'pagetitle' => $pagetitle,
                'theme' => $objCI->user_interface->get_theme(),
                'browsers' => $objCI->config->item('browser_version'),
                'lang_form' => $arrFormLang,
            );
            
            /* Add the connections */
            if($logged_in) {
                $arrDisplay['connections'] = $arrConnections;
                $arrDisplay['all_db_types'] = $objCI->database->get_db_types(false);
                $arrDisplay['new_connection'] = $new_connection_page;
            }
            
            $main = $objCI->load->view($template, $arrDisplay, true);

            /* Put the stuff loaded from the controller into the main - use the Smarty format for safety */
            $output = str_replace('{$content}', $objCI->output->get_output(), $main);

            /* Remove Smarty style comment tags - things between {* and *} */
            $output = preg_replace('/((\{\*)(.*?)(\*\}))/', '', $output);

            /* Remove excess whitespace in the HTML - straight from Smarty function */
            if($objCI->config->item('strip_source_whitespace')) { $output = preg_replace('!\s+!u', ' ', $output); }

            /* Output the display */
            $objCI->output->_display($output);
        
        }
    }
    
    
}

?>